package org.jfinanceobserver.persistence;

import java.util.HashMap;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import org.eclipse.persistence.config.PersistenceUnitProperties;

/**
 * This is the Connection factory
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-14 15:47:43 +0000 (Thu, 14 Feb 2013) $ $LastChangedRevision: 34 $
 *
 * @since Feb 10, 2013
 */
public class ConnFactory {

    private static ConnFactory singleton;
    private EntityManagerFactory factory = null;
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/finance";
    private String user = "finance";
    private String passwd = "mypass";

    private ConnFactory() {
    }

    /**
     * Return a ConnFactory
     *
     * @return a ConnFactory
     */
    public static synchronized ConnFactory getInstance() {
        if (singleton == null) {
            singleton = new ConnFactory();
        }
        return singleton;
    }

    /**
     * Get the Entity Manager Factory
     *
     * @return the Entity Manager Factory
     */
    public EntityManagerFactory getFactory() {
        return factory;
    }

    /**
     * Connect to the EntityManager
     */
    public void connect(){
        System.out.println("Connecting");
        HashMap<String, String> properties = new HashMap<>();
        properties.put(PersistenceUnitProperties.JDBC_URL, getUrl().trim() + "?autoReconnect=true");
        properties.put(PersistenceUnitProperties.JDBC_PASSWORD, getPasswd());
        properties.put(PersistenceUnitProperties.JDBC_DRIVER, getDriver());
        properties.put(PersistenceUnitProperties.JDBC_USER, getUser());
        factory = Persistence.createEntityManagerFactory(("org.jfinanceobserver"), properties);
    }

    /**
     * Close the EntityManager
     */
    public void close() {
        if (factory != null && factory.isOpen()) {
            factory.close();
            factory = null;
        }
    }

    /**
     * Get the MySQL Driver
     *
     * @return the MySQL Driver
     */
    public String getDriver() {
        return driver;
    }

    /**
     * Set the MySQL Driver
     *
     * @param driver the MySQL Driver
     */
    public void setDriver(String driver) {
        this.driver = driver;
    }

    /**
     * Get the MySQL URL
     *
     * @return the MySQL URL
     */
    public String getUrl() {
        return url;
    }

    /**
     * Set the MySQL URL
     *
     * @param url the MySQL URL
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * Get the MySQL User
     *
     * @return the MySQL User
     */
    public String getUser() {
        return user;
    }

    /**
     * Set the MySQL User
     *
     * @param user the MySQL User
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * Get the MySQL Password
     *
     * @return the MySQL Password
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * Set the MySQL Password
     *
     * @param passwd the MySQL Password
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
}
